<?php

namespace App\Jobs;

use App\Models\EstateRole;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class EstateRoleJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }


    /**
     * 2021/3/20--16:56
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:handle
     * @effect:权限修改队列
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
            $role_model = new EstateRole();
            $role_model = $role_model->find($this->data['id']);
            $role_model->menus()->sync($this->data['menus'] ?? []);
            $role_model->permissions()->sync($this->data['permissions']);
            DB::commit();
        } catch (\Exception $e) {
            Log::channel('EstateRoleJob')->debug('EstateRoleJob:' . json_encode($e->getMessage()));
            DB::rollBack();
            $this->release(5);
        }
    }
}
